var mysql = require('mysql');

var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'zhixin1',
    timezone: "08:00"
})


function doSql(sql, params = []) {

    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, connection) {
            connection.query(sql, params, (error, resultes, fileds) => {
                if (err) {
                    reject(err)
                }
                resolve(resultes)
            })
            connection.release();
        })

    })
}

function hand(data) {
    return {
        success: 'true',
        'code': 20000,
        errorCode: 0,
        msg: '数据请求成功',
        data: data
    }
}

function heal(data, total) {
    return {
        'success': 'true',
        'code': 20000,
        'errorCode': 0,
        'msg': '数据请求成功',
        'data': data,
        total: total,
    }
}

function nohand(data) {
    return {
        success: 'true',
        'code': 20000,
        errorCode: 222000,
        msg: '数据请求成功但不返回数据'
    }
}
//失败
var errFn = (err) => {
    return {
        success: false,
        errcode: 0,
        msg: '数据请求失败',
        data: err,
        code:0000
    }
}
exports.doSql = doSql;
exports.heal = heal;
exports.hand = hand;
exports.nohand = nohand;
exports.errFn = errFn;